<template>
  <div class="login">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <h1>系 统 登 录</h1>
      <el-form-item prop="username">
        <el-input placeholder="请输入账号" style="width: 300px;backgroundColor:#2a3443;color:white;" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          style="width: 300px;backgroundColor:#2a3443;color:white;"
          v-model="ruleForm.password"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" style="width:100%" @click="logiTo">一键登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getMenu } from '@/api/login';
export default {
  name: 'Login',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    login() {
      getMenu(this.ruleForm).then(res => {
        res = res.data;
        if (res.code === 20000) {
          this.$store.commit('tab/clearMenu');
          this.$store.commit('tab/setMenu', res.data.menu);
          this.$store.commit('user/setToken', res.data.token);
          this.$store.commit('tab/addMenu', this.$router);
          this.$router.push({ name: 'home' });
        } else {
          this.$message.warning(res.data.message);
        }
      });
    },
    logiTo() {
      this.ruleForm.username = 'admin';
      this.ruleForm.password = '123456';
      setTimeout(() => {
        this.login();
      }, 2000);
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  background-color: #2f3a4b;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /dee/.el-form {
    width: 450px !important;
    height: 500px;
  }
  h1 {
    color: white;
    font-weight: bold;
    font-size: 28px;
    text-align: center;
    margin-bottom: 25px;
  }
}
</style>
